import Vue from "vue";
import Router from "vue-router";
import Login from "./components/login.vue";
import Index from "./components/Index.vue";
import Error from "./components/Error.vue";
import Welcome from "./components/Welcome.vue";
import User from "./components/user/User.vue";
import Roler from "./components/roler/Roler.vue";
import Menu from "./components/menu/Menu.vue";

Vue.use(Router);

const router = new Router({
  mode: "history",
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "login",
      component: Login,
    },
    {
      path: "/index",
      name: "index",
      component: Index,
      redirect: "/welcome",
      children: [
        {
          path: "/welcome",
          component: Welcome,
        },
        {
          path: "/user",
          component: User,
        },
        {
          path: "/roler",
          component: Roler
        },
        {
          path: "/menu",
          component: Menu
        }
      ],
    },
    {
      path: "/error",
      name: "error",
      component: Error,
    },
  ],
});

//路由守卫
router.beforeEach((to,from,next)=>{
  if(to.path==='/login') return next();
  const tokenStr= window.sessionStorage.getItem('token');
  if(!tokenStr) return next('/login');
  next();
})

export default router;
